def options(opt):
    opt.load('compiler_cxx compiler_fc gnu_dirs FTensor')


def configure(conf):
    configure_variant(conf);

def configure_variant(conf):
    conf.load('compiler_cxx compiler_fc gnu_dirs FTensor')

    # Optimization flags
    optimize_msg="Checking for optimization flag "
    optimize_fragment="int main() {}\n"
    optimize_flags=['-Ofast','-O3','-O2','-O']
    for flag in optimize_flags:
        try:
            conf.check_cxx(msg=optimize_msg+flag, fragment=optimize_fragment,
                           cxxflags=flag, uselib_store='optimize')
        except conf.errors.ConfigurationError:
            continue
        else:
            found_optimize=True
            break


def build(bld):
    default_flags=['-Wall', '-Wextra', '-Wconversion']
    use_array=['optimize','FTensor']

    okada_sources=['Okada/Okada.cxx',
                   'Okada/dc3d.cxx',
                   'Okada/displacement.cxx',
                   'Okada/source_contribution.cxx',
                   'Okada/Iteration_Params/Iteration_Params.cxx',
                   'Okada/Iteration_Params/ua.cxx',
                   'Okada/Iteration_Params/ub.cxx',
                   'Okada/Iteration_Params/uc.cxx']
    bld.stlib(
        source=okada_sources,
        target='okada',
        name='okada_st',
        install_path=bld.env.LIBDIR,
        cxxflags = default_flags,
        use=use_array
        )

    bld.shlib(
        source=okada_sources,
        target='okada',
        install_path=bld.env.LIBDIR,
        cxxflags = default_flags,
        use=use_array
        )

    bld.install_files(bld.env.INCLUDEDIR,'Okada.hxx')

    bld.program(
        features=['c','fc','fcprogram'],
        source=['fortran.f90', 'okada.f90','DC3D.f'],
        use=['optimize'],
        fcflags=default_flags,
        target='fortran',
        install_path=None
        )

    bld.program(
        source=['main.cxx'],
        target='cxx',
        use=['okada_st','FTensor','optimize'],
        cxxflags=default_flags,
        install_path=None
        )
